package cn.aishua.data;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class HotsDataParser {

	public ArrayList<HotsVendor> cuisineVendors = new ArrayList<HotsVendor>();
	public ArrayList<HotsVendor> entertainmentVendors = new ArrayList<HotsVendor>();
	public ArrayList<HotsVendor> lifeVendors = new ArrayList<HotsVendor>();
	public ArrayList<HotsVendor> hotelVendors = new ArrayList<HotsVendor>();
	public ArrayList<HotsVendor> marketVendors = new ArrayList<HotsVendor>();
	
	public int pos;
	public int count;
	public ArrayList<HotsVendor> vendors;
	
	public String cuisineCategoryStoresCount;
	public String hotelCategoryStoresCount;
	public String lifeCategoryStoresCount;
	public String marketCategoryStoresCount;
	public String entertainmentCategoryStoresCount;
	
	public HotsDataParser(String data){
		parse(data);
	}
	
	private void parse(String data){
		try {
			Log.d("test", data);
			JSONObject obj = new JSONObject(data);
			String[] categories = {"cuisine_category_stores",
									"entertainment_category_stores",
									"life_category_stores",
									"hotel_category_stores",
									"market_category_stores"};
			
			cuisineCategoryStoresCount = obj.getString("cuisine_category_stores_count");
			hotelCategoryStoresCount = obj.getString("hotel_category_stores_count");
			lifeCategoryStoresCount = obj.getString("life_category_stores_count");
			marketCategoryStoresCount = obj.getString("market_category_stores_count");
			entertainmentCategoryStoresCount = obj.getString("entertainment_stores_merchant_count");
			
			for(int i = 0;i<categories.length;i++){
				Log.d("test", "categories " + categories[i]);
				JSONArray carray = obj.getJSONArray(categories[i]);
				for(int j = 0;j<carray.length();j++){
					JSONObject o = carray.getJSONObject(j);
					Log.d("test", "o id " + o.getInt("id"));
					if(categories[i].equals("cuisine_category_stores")){
						cuisineVendors.add(makeVendor(o));
						continue;
					}
					if(categories[i].equals("entertainment_category_stores")){
						entertainmentVendors.add(makeVendor(o));
						continue;
					}
					if(categories[i].equals("life_category_stores")){
						lifeVendors.add(makeVendor(o));
						continue;
					}
					if(categories[i].equals("hotel_category_stores")){
						hotelVendors.add(makeVendor(o));
						continue;
					}
					if(categories[i].equals("market_category_stores")){
						marketVendors.add(makeVendor(o));
						continue;
					}
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	private HotsVendor makeVendor(JSONObject o){
		HotsVendor v = new HotsVendor();
		try {
			v.id = o.getInt("id");
			v.name = o.getString("name");
			v.average_price = (float) o.getDouble("average_price");
			v.max_customer_rate = (float) o.getDouble("max_customer_rate");
			v.file_url = o.getString("file_url");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return v;
	}
	
	public static class HotsVendor{
		public int id;
		public String name;
		public float average_price;
		public float max_customer_rate;
		public String file_url;
	}
	
}
